package com.lw.extraceim.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.lw.extraceim.R;
import com.lw.extraceim.loader.UserLoader;
import com.lw.extraceim.model.UserInfo;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.base.BaseActivity;
import com.lw.extraceim.ui.activity.es.WriteExpressSheetActivity;
import com.lw.extraceim.util.TextUtil;
import com.lw.extraceim.view.ExtraceToolbar;

import java.util.Objects;

import okhttp3.Call;

public class SignActivity extends BaseActivity {
    private static final String TAG = "SignActivity";

    private ExtraceToolbar mToolbar;
    //    private EditText mEtSignNum;
    private EditText mEtSignName;
    private EditText mEtSignPhone;
    private EditText mEtSignPassword;
    private EditText mEtSignConfirmPassword;
    private EditText mEtSignCode;
    private Button mGetCodeButton;
    private Button mSignRegistButton;

    private UserInfo userInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_sign);
        mToolbar = findViewById(R.id.toolbar);
        initToolBar();
//        mEtSignNum = findViewById(R.id.et_sign_num);
        mEtSignName = findViewById(R.id.et_sign_name);
        mEtSignPhone = findViewById(R.id.et_sign_phone);
        mEtSignPassword = findViewById(R.id.et_sign_password);
        mEtSignConfirmPassword = findViewById(R.id.et_confirm_password);
        mEtSignCode = findViewById(R.id.et_sign_code);
        mGetCodeButton = findViewById(R.id.btn_getcode);
        mSignRegistButton = findViewById(R.id.btn_sign);
    }
    /**
     * 初始化toolBar
     */
    protected void initToolBar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setBackButtonOnClickListerner(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SignActivity.this.finish();
            }
        });
    }
    @Override
    public void initData() {

    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void initEvent() {
        mSignRegistButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();//注册
            }
        });
    }



    /**
     * 注册
     */
    public void register() {
        if (checkInput()) {
            UserLoader userLoader = new UserLoader(this);
            userLoader.register(userInfo, new HttpResponseParamCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    alert("网络错误" + e.getMessage());
                }

                @Override
                public void onResponse(HttpResponseParam response, int id) {
                    //注册成功后自动跳转到登录界面
                    if (response.code.getCode() == ResponseCode.SUCESS) {
                        Log.d(TAG, "onResponse: " + "成功");
                        showDialog("提示", "注册成功", "确认", null, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LoginActivity.actionStart(SignActivity.this);
                                finish();
                            }
                        }, null);
                    } else {
                        if (response.code.getMessage() != null) {
                            alert(response.code.getMessage());
                        }else{
                            alert("注册失败");
                        }
                    }
                }
            });
        }

    }


    @Override
    public void onBackPressed() {
        this.finish();
    }


    /**
     * 检查输入
     *
     * @return
     */
    public boolean checkInput() {
        userInfo = new UserInfo();
        String name = mEtSignName.getText().toString().trim();
        if (!TextUtil.isNull(name)) {
            userInfo.setName(name);
        } else {
            toast("姓名不能为空");
            return false;
        }

        String phone = mEtSignPhone.getText().toString().trim();
        if (!TextUtil.isNull(phone) && TextUtil.isPhone(phone)) {
            userInfo.setTelCode(phone);
        } else {
            toast("请输入正确的电话号码！");
            return false;
        }

        String password = mEtSignPassword.getText().toString();
        String confirmPassword = mEtSignConfirmPassword.getText().toString();
        if (TextUtil.isNull(password)) {
            toast("请输入密码！");
            return false;
        } else if (Objects.equals(password, confirmPassword)) {
            userInfo.setPassword(password);
        } else {
            toast("两次密码输入不一致！");
            return false;
        }
        //验证验证码
        return true;
    }

}
